package org.example.service.impl;

import org.example.mapper.CustomerMapper;
import org.example.mapper.HairTypeMapper;
import org.example.mapper.RecordMapper;
import org.example.model.Customer;
import org.example.model.CustomerExample;
import org.example.model.HairType;
import org.example.model.Record;
import org.example.service.CustomerService;
import org.example.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private HairTypeMapper hairTypeMapper;

    @Autowired
    private RecordMapper recordMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${redis.database}")
    private String REDIS_DATABASE;

    @Value("${redis.key.customer}")
    private String REDIS_KEY_CUSTOMER;

    @Override
    public List<Customer> listAll() {
        return customerMapper.selectByExample(new CustomerExample());
    }

    @Override
    public Customer getById(Long id) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_CUSTOMER + ":" + id;
        Customer customer = (Customer) redisUtil.get(key);
        if (customer == null) {
            Customer customer1 = customerMapper.selectByPrimaryKey(id);
            redisUtil.set(key, customer1);
            return customer1;
        } else {
            return customer;
        }
    }

    @Override
    public int create(Customer customer) {
        //考虑同一个手机是否注册过
        customer.setId(null);
        customer.setBalance(BigDecimal.valueOf(0));
        customer.setCardDate(new Date());
        customer.setStatus(1);
        customer.setCreator("qcl");
        customer.setCreateTime(new Date());
        return customerMapper.insertSelective(customer);
    }

    @Override
    public int update(Long id, Customer customer) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_CUSTOMER + ":" + customer.getPhoneNum();
        customer.setId(id);
        int result = customerMapper.updateByPrimaryKeySelective(customer);
        System.out.println(result);
        if (result == 1) {
            redisUtil.del(key);
        }
        return result;
    }

    @Override
    public int delete(Long id) {
        Customer customer = customerMapper.selectByPrimaryKey(id);
        String key = REDIS_DATABASE + ":" + REDIS_KEY_CUSTOMER + ":" + customer.getPhoneNum();
        int result = customerMapper.deleteByPrimaryKey(id);
        System.out.println(result);
        if (result == 1) {
            redisUtil.del(key);
        }
        return result;
    }

    @Override
    public List<Customer> getByCondition(int phoneNum) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_CUSTOMER + ":" + phoneNum;
        Customer customer = (Customer) redisUtil.get(key);
        if (customer == null) {
            CustomerExample customerExample = new CustomerExample();
            customerExample.createCriteria().andPhoneNumEqualTo(phoneNum);
            List<Customer> customerList = customerMapper.selectByExample(customerExample);
            if (customerList.size() == 1) {
                redisUtil.set(key, customerList.get(0));
            } else {
                //判断是否有2个以上同一手机号的账户  会存在这种情况  暂时不考虑一个手机号先后被2个人同时使用的情况Ï
                return null;
            }
            return customerList;
        } else {
            return List.of(customer);
        }
    }

    @Override
    public String consume(Long id, Long selectId) {
        HairType hairType = hairTypeMapper.selectByPrimaryKey(selectId);
        Customer customer = customerMapper.selectByPrimaryKey(id);
        if (customer.getBalance().subtract(hairType.getPrice()).compareTo(BigDecimal.ZERO) < 0) {
            return "余额不足";
        } else {
            BigDecimal balance = customer.getBalance().subtract(hairType.getPrice());
            customer.setBalance(balance);
            int result = customerMapper.updateByPrimaryKeySelective(customer);
            if (result == 1) {
                Record record = new Record();
                record.setId(null);
                record.setCustomerId(id);
                record.setName(customer.getName());
                record.setPhoneNum(customer.getPhoneNum());
                record.setCardNum(customer.getCardNum());
                record.setConsumeDate(new Date());
                record.setConsume(hairType.getPrice());
                record.setBalance(balance);
                record.setCreateTime(new Date());
                record.setCreator("qcl");
                int recordResult = recordMapper.insert(record);
                if (recordResult != 1) {
                    System.out.println("消费记录更新失败！");
                }
                return "扣款成功";
            } else {
                return "扣款失败";
            }
        }
    }

    @Override
    public int recharge(Long id, BigDecimal amount) {
        Customer customer = customerMapper.selectByPrimaryKey(id);
        customer.setBalance(customer.getBalance().add(amount));
        return customerMapper.updateByPrimaryKeySelective(customer);
    }


}
